<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>个人设置 - 智慧农业管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
        }

        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #4caf50 0%, #81c784 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            color: #334155;
        }

        .container {
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            width: 500px;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #4caf50, #81c784);
        }

        .header {
            text-align: center;
            margin-bottom: 25px;
        }

        .header h1 {
            font-size: 24px;
            color: #2e7d32;
            font-weight: 600;
        }

        .header p {
            font-size: 14px;
            color: #6b7280;
            margin-top: 5px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: #374151;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #4caf50;
            box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
        }

        .error-message {
            color: #ef4444;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #10b981;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #4caf50 0%, #81c784 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 5px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
        }

        .btn:active {
            transform: translateY(0);
        }

        .btn-secondary {
            background: #6b7280;
            margin-top: 10px;
        }

        .btn-secondary:hover {
            box-shadow: 0 4px 12px rgba(107, 114, 128, 0.3);
        }

        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #4caf50;
            text-decoration: none;
            font-size: 14px;
        }

        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>个人设置</h1>
        <p>修改您的个人信息</p>
    </div>

    <form id="profileForm">
        <div class="form-group">
            <label for="loginAccount">登录账号</label>
            <input type="text" id="loginAccount" name="loginAccount" readonly>
        </div>

        <div class="form-group">
            <label for="userName">姓名</label>
            <input type="text" id="userName" name="userName" required>
        </div>

        <div class="form-group">
            <label for="oldPassword">当前密码</label>
            <input type="password" id="oldPassword" name="oldPassword">
        </div>

        <div class="form-group">
            <label for="newPassword">新密码(留空表示不修改)</label>
            <input type="password" id="newPassword" name="newPassword">
        </div>

        <div class="form-group">
            <label for="confirmPassword">确认新密码</label>
            <input type="password" id="confirmPassword" name="confirmPassword">
        </div>

        <div class="error-message" id="error"></div>
        <div class="success-message" id="success"></div>

        <button type="submit" class="btn">保存修改</button>
        <button type="button" class="btn btn-secondary" onclick="history.back()">返回</button>
    </form>

    <a href="main.jsp" class="back-link">返回主页</a>
</div>

<script>
    // 获取当前用户信息
    document.addEventListener('DOMContentLoaded', function() {
        fetch('userServlet?actionType=getCurrentUser')
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                document.getElementById('loginAccount').value = data.user.loginAccount;
                document.getElementById('userName').value = data.user.userName || '';
            } else {
                document.getElementById('error').textContent = '获取用户信息失败';
                document.getElementById('error').style.display = 'block';
            }
        })
        .catch(error => {
            console.error('获取用户信息失败:', error);
            document.getElementById('error').textContent = '获取用户信息失败';
            document.getElementById('error').style.display = 'block';
        });
    });

    // 表单提交
    document.getElementById('profileForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const loginAccount = document.getElementById('loginAccount').value;
        const userName = document.getElementById('userName').value;
        const oldPassword = document.getElementById('oldPassword').value;
        const newPassword = document.getElementById('newPassword').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const errorMsg = document.getElementById('error');
        const successMsg = document.getElementById('success');

        // 清除之前的消息
        errorMsg.style.display = 'none';
        successMsg.style.display = 'none';

        // 表单验证
        if (!userName) {
            errorMsg.textContent = '姓名不能为空';
            errorMsg.style.display = 'block';
            return;
        }

        // 如果要修改密码，需要验证当前密码和新密码
        if (newPassword) {
            if (!oldPassword) {
                errorMsg.textContent = '修改密码需要输入当前密码';
                errorMsg.style.display = 'block';
                return;
            }

            if (newPassword.length < 6) {
                errorMsg.textContent = '新密码长度不能少于6位';
                errorMsg.style.display = 'block';
                return;
            }

            if (newPassword !== confirmPassword) {
                errorMsg.textContent = '两次输入的新密码不一致';
                errorMsg.style.display = 'block';
                return;
            }
        }

        // 提交表单
        const formData = new URLSearchParams();
        formData.append('actionType', 'updateProfile');
        formData.append('loginAccount', loginAccount);
        formData.append('userName', userName);

        if (oldPassword) {
            formData.append('oldPassword', oldPassword);
        }

        if (newPassword) {
            formData.append('newPassword', newPassword);
        }

        fetch('userServlet', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: formData.toString()
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                successMsg.textContent = data.message;
                successMsg.style.display = 'block';

                // 3秒后返回主页
                setTimeout(function() {
                    window.location.href = 'main.jsp';
                }, 3000);
            } else {
                errorMsg.textContent = data.message;
                errorMsg.style.display = 'block';
            }
        })
        .catch(error => {
            console.error('更新用户信息失败:', error);
            errorMsg.textContent = '更新失败，请稍后再试';
            errorMsg.style.display = 'block';
        });
    });
</script>
</body>
</html>
